import {
  getSetting,
  chooseAddress,
  openSetting,
  showModal,
  showToast
} from "../../utils/asyncWX.js"
import regeneratorRuntime, {
  async
} from "../../lib/runtime/runtime.js"
import {
  request
} from "../../request/index.js"
//调用小程序内置的api 获取用户的收货地址

Page({
  data: {
    address: {},
    carts: [],
    totalPrice: 0,
    totalNum: 0
  },
  onShow: function () {
    let carts = wx.getStorageSync("cart") || [];
    carts=carts.filter(ele=>ele.checked);
    let totalPrice = 0;
    let totalNum = 0;
    carts.forEach(ele => {
        totalPrice += ele.num * ele.goods_price;
        totalNum += ele.num;
      });
    wx.setStorageSync("cart", carts)
    this.setData({
      address: wx.getStorageSync("address"),
      carts,
      totalPrice,
      totalNum
    })
  },
  handleOrderPay(){
    const token=wx.getStorageSync("token");
    if(!token){
      wx.navigateTo({
        url: '/pages/auth/index'
      });
      return;
    }  
    //创建订单
      // 3.1 准备 请求头参数
      // const header = { Authorization: token };
      // // 3.2 准备 请求体参数
      // const order_price = this.data.totalPrice;
      // const consignee_addr = this.data.address.all;
      // const cart = this.data.cart;
      // let goods = [];
      // cart.forEach(v => goods.push({
      //   goods_id: v.goods_id,
      //   goods_number: v.num,
      //   goods_price: v.goods_price
      // }))
      // const orderParams = { order_price, consignee_addr, goods };
      // const res=await request({url:"/my/orders/create",methods:"POST",data:orderParams,header})
  }
})